package com.tjsinfo.tjsoa.bylaws.action;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.bylaws.bo.IBylawsBO;
import com.tjsinfo.tjsoa.bylaws.vo.Bylaws;
import com.tjsinfo.tjsoa.common.DealString;

import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;
import com.tjsinfo.tjsoa.workplan.bo.IWorkPlanMainBo;

public class NewAction extends Action {

	private IBylawsBO bylawsManager;
	private IWorkPlanMainBo workPlanMainBo;
	
	public IWorkPlanMainBo getWorkPlanMainBo() {
		return workPlanMainBo;
	}

	public void setWorkPlanMainBo(IWorkPlanMainBo workPlanMainBo) {
		this.workPlanMainBo = workPlanMainBo;
	}
	public void setBylawsManager(IBylawsBO bylawsManager){
		this.bylawsManager=bylawsManager;
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
	
		HttpSession session=request.getSession();
		User user = (User)session.getAttribute("user");

		DealString ds = new DealString();

	    String htmlStr = request.getParameter("contest"); //含html标签的字符串    
	    String textStr ="";    
	    java.util.regex.Pattern p_script;    
	    java.util.regex.Matcher m_script;    
	    java.util.regex.Pattern p_style;    
	    java.util.regex.Matcher m_style;    
	    java.util.regex.Pattern p_html;    
	    java.util.regex.Matcher m_html;    
	          
	      
	    String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }    
	    String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }    
	    String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式    
	           
	    p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);    
	    m_script = p_script.matcher(htmlStr);    
	    htmlStr = m_script.replaceAll(""); //过滤script标签    
	   
	    p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);    
	    m_style = p_style.matcher(htmlStr);    
	    htmlStr = m_style.replaceAll(""); //过滤style标签    
	    p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);    
	    m_html = p_html.matcher(htmlStr);    
	    htmlStr = m_html.replaceAll(""); //过滤html标签    
	    textStr = htmlStr;   
	   
    	// 设置各项值并保存
	    WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
	    Long flowid = 0l;
	    if("3".equals(request.getParameter("cid"))){
	    	flowid =  workFlowTemplate.creatProcessInstance("审批", request.getParameter("ids"), "规章制度");
	    }else{
	    	flowid =  workFlowTemplate.creatProcessInstance("审批", request.getParameter("ids"), "通知");
	    }
		Bylaws bylaws = new Bylaws();
		bylaws.setWenzhang(textStr);
		bylaws.setFlowid(Integer.parseInt(flowid.toString()));
		bylaws.setCid(request.getParameter("cid"));
		bylaws.setState(request.getParameter("state"));
		bylaws.setTitle(request.getParameter("title"));
		bylaws.setContent(request.getParameter("contest"));
		bylaws.setResume(request.getParameter("resume"));
		bylaws.setUserId(request.getParameter("userId"));
		bylaws.setAuditMan(request.getParameter("auditMan"));
		bylaws.setAuditIdea(request.getParameter("auditIdea"));
		bylaws.setAuditing_sign("0");


		if(request.getParameter("createDate")==null||request.getParameter("createDate").equals("")){
			bylaws.setCreateDate(ds.FormatDate(new Date()));
		}else{
			bylaws.setCreateDate(request.getParameter("createDate"));

		}
		if(request.getParameter("pic")==null||request.getParameter("pic").equals("")){
			bylaws.setPic("1");
		}else{
			String smallPic=request.getParameter("pic");
			smallPic=smallPic.replaceAll("<p>","");
			smallPic=smallPic.replaceAll("</p>","");
			bylaws.setPic(smallPic);
		}
		
		bylawsManager.save(bylaws);
		List result = null;
		
		result=bylawsManager.findByTwoProperty("cid", request.getParameter("cid"),"userId",String.valueOf(user.getUsername()));
			
		request.setAttribute("result", result);		
		request.setAttribute("cid", request.getParameter("cid"));
		request.setAttribute("returnString", "ok");
		workPlanMainBo.selectMapWorkPlanMain(request);
		// 转向，并传递提示语
		if("3".equals(request.getParameter("cid"))){
			request.setAttribute("linkurl", "tjsinfo/tjsoa/bylaws/bylawscidback.do?cid=3");
		}else{
			request.setAttribute("linkurl", "tjsinfo/tjsoa/bylaws/bylawscidback.do?cid=2");
		}
		
		request.setAttribute("message", "bylaws.message.save");
		return mapping.findForward("success");
	}

	


}
